package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserGoods
//@description: 创建UserGoods记录
//@param: userGoods model.UserGoods
//@return: err error

func CreateUserGoods(userGoods model.UserGoods) (err error) {
	err = global.GVA_DB.Create(&userGoods).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserGoods
//@description: 删除UserGoods记录
//@param: userGoods model.UserGoods
//@return: err error

func DeleteUserGoods(userGoods model.UserGoods) (err error) {
	err = global.GVA_DB.Delete(&userGoods).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserGoodsByIds
//@description: 批量删除UserGoods记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserGoodsByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserGoods{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserGoods
//@description: 更新UserGoods记录
//@param: userGoods *model.UserGoods
//@return: err error

func UpdateUserGoods(userGoods model.UserGoods) (err error) {
	err = global.GVA_DB.Save(&userGoods).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserGoods
//@description: 根据id获取UserGoods记录
//@param: id uint
//@return: err error, userGoods model.UserGoods

func GetUserGoods(id uint) (err error, userGoods model.UserGoods) {
	err = global.GVA_DB.Where("id = ?", id).Preload(`CWorkInfo`).Preload(`CProductInfo`).Preload(`UserGoodsPic`).First(&userGoods).Error
	return
}

func GetUserGoodsQy(info request.UserGoodsSearch,order string)(err error,userGoodss []model.UserGoods,total int64){
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Table(`user_goods`).Where(`status=3`).Group(`user_id`)
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`Users`).Order(order).Find(&userGoodss).Error
	return err,userGoodss,total
}


func GetGoodsTotalInfo(info request.UserGoodsSearch)(applyCount,overCount,allCount int64){
	db := global.GVA_DB.Model(&model.UserGoods{})
	db.Count(&allCount)
	db = global.GVA_DB.Model(&model.UserGoods{})
	db.Where("status = ? ",1).Count(&applyCount)
	db = global.GVA_DB.Model(&model.UserGoods{})
	db.Where("status = ?",2).Count(&overCount)
	db = global.GVA_DB.Model(&model.UserGoods{})
	db.Where("status = ?",3).Count(&allCount)
	return
}

func GetUserGoodsTotalInfo(info request.UserGoodsSearch)(applyCount,overCount,allCount int64){
	db := global.GVA_DB.Model(&model.UserGoods{})

	db.Count(&allCount)
	db = global.GVA_DB.Model(&model.UserGoods{})
	db.Where("status = ? and user_id = ?",1,info.UserId).Count(&applyCount)
	db = global.GVA_DB.Model(&model.UserGoods{})
	db.Where("status = ? and user_id = ?",2,info.UserId).Count(&overCount)
	db = global.GVA_DB.Model(&model.UserGoods{})
	db.Where("status = ? and user_id = ?",3,info.UserId).Count(&allCount)
	return
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserGoodsInfoList
//@description: 分页获取UserGoods记录
//@param: info request.UserGoodsSearch
//@return: err error, list interface{}, total int64

func GetUserGoodsInfoList(info request.UserGoodsSearch,order string) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.UserGoods{})
	var userGoodss []model.UserGoods
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.IsQa != nil {
		db = db.Where("`is_qa` = ?",info.IsQa)
	}
	if info.Status != 0 {
		db = db.Where("`status` = ?",info.Status)
	}
	if info.UserId != 0 {
		db = db.Where("`user_id` = ?",info.UserId)
	}
	if info.CWork  != 0 {
		db = db.Where("`c_work` = ?",info.CWork)
	}

	if info.CProduct != 0{
		db = db.Where("`c_product` = ?",info.CProduct)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload(`Users`).Preload(`CWorkInfo`).Preload(`CProductInfo`).Order(order).Preload(`UserGoodsPic`).Find(&userGoodss).Error
	return err, userGoodss, total
}